#!/bin/bash -ex

find_latest_qemu_version() {
    git ls-remote --tags git://git.qemu.org/qemu.git \
        | grep -v '\-rc' \
        | grep -oh 'v[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}\(\.[0-9]\{0,2\}\)\?' \
        | sort --version-sort -r \
        | head -n 1
}

#VERSION=v2.10.1
VERSION=$(find_latest_qemu_version)
git clone --depth=1 -b "$VERSION" git://git.qemu-project.org/qemu.git

source ctf-tools-venv-activate

prefix="--prefix=$(pwd)"
python="--python=$(which python)"

cd qemu
if ! ./configure "$prefix" "$python"; then
    echo "Updating QEMU submodules in case dependencies are missing"
    git submodule init
    git submodule update --recursive
    ./configure "$prefix" "$python"
fi
make -j $(nproc)
make install
